package com.jeasonzhao.report.model;

import java.util.Vector;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.utils.DataTypes;

public class DictItemCollection extends Vector<DictItem>
{
    private static final long serialVersionUID = 1L;
    private static final boolean USING_CACHE = true;
    public static final String NULL = "#NULL#";
    public static final String DELIMITER = "|";
    public static final String SUB_DELIMITER = ":";
    private DataTypes m_dataType = DataTypes.STRING;
    public DictItemCollection(DataTypes dt)
    {
        m_dataType = null == dt ? DataTypes.STRING : dt;
    }

    public void addDictItem(Object pid,Object key,String strName)
    {
        if(null == key)
        {
            return;
        }
        if(null == strName)
        {
            strName = key.toString();
        }
        this.addElement(new DictItem(pid,key,strName));
    }

    public void addAll(DictItemCollection colls)
    {
        if(null == colls)
        {
            return;
        }
        for(int n = 0;n < colls.size();n++)
        {
            this.addElement(colls.elementAt(n));
        }
    }

    public DictItemCollection getItemFromParentID(Object pdi)
    {
        DictItemCollection ret = new DictItemCollection(this.m_dataType);
        pdi = m_dataType.castValue(pdi);
        for(int n = 0;n < this.size();n++)
        {
            DictItem item = this.elementAt(n);
            if((null == pdi && item.getParentKey() == null) || (null != pdi && pdi.equals(item.getParentKey())))
            {
                ret.addElement(item);
            }
        }
        return ret.size() < 1 ? null : ret;
    }

    private java.util.Hashtable<Object,DictItem> m_hashKeys = USING_CACHE ? new java.util.Hashtable<Object,DictItem>() : null;
    public DictItem getItemFromID(Object key)
    {
        if(null == key)
        {
            return null;
        }
        try
        {
            key = m_dataType.castValue(key);
            if(null == key)
            {
                return null;
            }
            if(this.m_dataType.equals(DataTypes.STRING))
            {
                key = key.toString().toLowerCase(); //Adjust the string keys
            }
            if(USING_CACHE && null != this.m_hashKeys && this.m_hashKeys.containsKey(key))
            {
                return m_hashKeys.get(key);
            }
            for(DictItem item : this)
            {
                if(this.m_dataType.equals(DataTypes.STRING))
                {
                    if(item.getKey() != null && item.getKey().toString().equalsIgnoreCase(key.toString()))
                    {
                        m_hashKeys.put(key,item);
                        return item;
                    }
                }
                else if(null != item && item.getKey() != null && item.getKey().equals(key))
                {
                    m_hashKeys.put(key,item);
                    return item;
                }
            }
        }
        catch(Exception ex)
        {
        }
        return null;
    }

    public int hashCode()
    {
        return super.hashCode();
    }

    public boolean equals(Object obj)
    {
        return Algorithms.areEquals(this,obj);
    }
}
